package cetcbigdata.da.business_environment.guangdong;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Spider extends Base {

    private static String baseRegion = "广东省";

    private static String baseModule = "营商环境";


    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        List<JSONObject> companyIndexList = getCompanyIndex();
        JSONArray cityList = getCityList();
        for (JSONObject companyIndex : companyIndexList){
            for (Object o : cityList){
                JSONObject city = (JSONObject) o;
                List<JSONObject> detailList = getDetail(city);
                //判断各个市是否包含区并把没有区的市进行合并
                if (detailList.size() != 0){
                    for (JSONObject detail : detailList){
                        //获取一级模块编码和各个市的所有区的编码
                        String code = companyIndex.getString("code");
                        String regCode = detail.getString("ORGAREACODE");
                        String name =  companyIndex.getString("name");
                        String module = baseModule + "-" + companyIndex.getString("name");
                        String region = baseRegion + "-" + city.getString("name") + "-" + detail.getString("ORGSNAME");
                        parseListPage(name ,module ,code ,region, regCode, recordSender,  fieldsList);
                    }
                }else {
                    String code = companyIndex.getString("code");
                    String regCode = city.getString("region");
                    String name =  companyIndex.getString("name");
                    String module = baseModule + "-" + companyIndex.getString("name");
                    String region = baseRegion + "-" + city.getString("name");
                    parseListPage(name ,module ,code ,region, regCode, recordSender,  fieldsList);
                }
            }
        }
    }

    @Override
    public JSONObject getDataDemo() {
        return processResult("xxx", "xxxxx", "xxxxxx",
                "xxxxx", new JSONObject(), "xxx", "xxx'");
    }

    private void parseListPage(String name ,String module ,String code, String region, String regCode,
                               RecordSender recordSender, List<Object> fieldsList){

        String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsBusiness/companySubNew";
        JSONObject header = new JSONObject();
        header.put("Referer", "https://www.gdzwfw.gov.cn/portal/legal/mattersLv2/hot?region=" + regCode + "&lv2code=" + code);
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        JSONObject params = new JSONObject();
        params.put("lv2code", code);
        params.put("region", regCode);

        //请求列表页
        String response = postFormReq(url, header, params);
        JSONObject responseObj = JSONObject.parseObject(response);
        if (responseObj.getString("statusCode").equals("200")){
            JSONArray list = responseObj.getJSONObject("data").getJSONObject("data").getJSONArray("items");
            for (Object o : list){
                JSONObject item = (JSONObject) o;
                //二级名称
                String secondName = item.getString("name");

                //获取详情页的模块编码
                JSONArray listChild = item.getJSONArray("items");
                for (Object oChild : listChild){
                    JSONObject itemChild = (JSONObject) oChild;
                    String id = itemChild.getString("id");
                    //三级名称
                    String thirdName = itemChild.getString("name");
                    //判断是否需要采集该模块
                    String Code = itemChild.getString("code");
                    if (null != Code && 0 != Code.length()){
                        JSONArray codeList = JSONArray.parseArray(Code);
                        for (Object c : codeList){
                            JSONObject cc = (JSONObject) c;
                            JSONArray taskCode = cc.getJSONArray("code");
                            try {
                                String  listUrl = "https://www.gdzwfw.gov.cn/portal/item/getMultiAuditItem_Cur";
                                JSONObject detaliHearder = new JSONObject();
                                detaliHearder.put("Referer", "https://www.gdzwfw.gov.cn/portal/lv3/hot?region=" + regCode + "&type=company&id=" + id + "&area_code=" + regCode + "&lv1_code=9300000022&lv1_name=%E8%AE%BE%E7%AB%8B%E7%99%BB%E8%AE%B0&lv2_name=%E4%B8%AA%E4%BA%BA%E7%8B%AC%E8%B5%84%E4%BC%81%E4%B8%9A%E8%AE%BE%E7%AB%8B%E7%99%BB%E8%AE%B0");
                                detaliHearder.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
                                JSONObject detaliParmas = new JSONObject();
                                detaliParmas.put("pageNum", 1);
                                detaliParmas.put("pageSize", taskCode.size());
                                detaliParmas.put("TASK_CODE", taskCode);
                                detaliParmas.put("AREA_CODE", "");
                                detaliParmas.put("IS_ONLINE", "");
                                //请求详情页
                                String listContentResponse = postFormReq(listUrl, detaliHearder, detaliParmas);
                                String finalModule = String.format("%s-%s-%s", module, secondName, thirdName);
                                getItemInfo(
                                        listUrl,listContentResponse,
                                        region, finalModule,
                                        recordSender, fieldsList
                                );

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }

    private void getItemInfo(String listUrl, String listRessponse,
                             String region, String module,
                             RecordSender recordSender, List<Object> fieldsList) {
        JSONObject response = JSONObject.parseObject(listRessponse);
        //获取事项名称和url
        JSONArray listItem = response.getJSONObject("data").getJSONObject("CUSTOM").getJSONArray("AUDIT_ITEMLIST");
        for (Object o : listItem){
            JSONObject itemObj = (JSONObject) o;
            JSONObject data = new JSONObject();
            data.put("事项名称", itemObj.getString("CATANAME"));
            String itemUrl = String.format("https://www.gdzwfw.gov.cn/portal/v2/guide/%s",
                    itemObj.getString("TASK_CODE"));
            data.put("网址", itemUrl);
           System.out.println(data.toString() + "--" + region + "===" +module);

//            processItem(
//                    processResult(listUrl, listRessponse, itemUrl, "", data, region, module),
//                    recordSender, fieldsList
//            );
        }
    }

    private static JSONArray getCityList(){
        String city = " [\n" +
                "{\"name\":\"广州市\",\"region\":\"440100\"},    \n" +
                "{\"name\":\"深圳市\",\"region\":\"440300\"}, \n" +
                "{\"name\":\"珠海市\",\"region\":\"440400\"},\n" +
                "{\"name\":\"汕头市\",\"region\":\"440500\"},\n" +
                "{\"name\":\"佛山市\",\"region\":\"440600\"},\n" +
                "{\"name\":\"韶关市\",\"region\":\"440200\"},\n" +
                "{\"name\":\"河源市\",\"region\":\"441600\"},\n" +
                "{\"name\":\"梅州市\",\"region\":\"441400\"},\n" +
                "{\"name\":\"惠州市\",\"region\":\"441300\"},\n" +
                "{\"name\":\"汕尾市\",\"region\":\"441500\"},\n" +
                "{\"name\":\"东莞市\",\"region\":\"441900\"},\n" +
                "{\"name\":\"中山市\",\"region\":\"442000\"},\n" +
                "{\"name\":\"江门市\",\"region\":\"440700\"},\n" +
                "{\"name\":\"阳江市\",\"region\":\"441700\"},\n" +
                "{\"name\":\"湛江市\",\"region\":\"440800\"},\n" +
                "{\"name\":\"茂名市\",\"region\":\"440900\"},\n" +
                "{\"name\":\"肇庆市\",\"region\":\"441200\"},\n" +
                "{\"name\":\"揭阳市\",\"region\":\"445200\"},\n" +
                "{\"name\":\"清远市\",\"region\":\"441800\"},\n" +
                "{\"name\":\"潮州市\",\"region\":\"445100\"},\n" +
                "{\"name\":\"云浮市\",\"region\":\"445300\"},\n" +
//                "{\"name\":\"横琴粤澳深度合作区\",\"region\":\"440405\"}\n" +
                " ]";
        return JSONArray.parseArray(city);
    }

    private static List<JSONObject> getDetail(JSONObject city) {
        String regCode = city.getString("region");
        String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsNav/getDetail";
        JSONObject header = new JSONObject();
        header.put("Referer", "https://www.gdzwfw.gov.cn/portal/lv3/hot?region=440000&type=company&id=9400000142&area_code=" +regCode + "lv1_code=9300000022&lv1_name=%E8%AE%BE%E7%AB%8B%E7%99%BB%E8%AE%B0&lv2_name=%E4%B8%AA%E4%BA%BA%E7%8B%AC%E8%B5%84%E4%BC%81%E4%B8%9A%E8%AE%BE%E7%AB%8B%E7%99%BB%E8%AE%B0");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        JSONObject params = new JSONObject();
        params.put("regCode", regCode);

        //获取各个市的所有区的源数据
        String response = postFormReq(url, header, params);
        JSONObject responseObj = JSONObject.parseObject(response);
        List<JSONObject> out = new ArrayList<>();
        if (responseObj.getString("statusCode").equals("200")){
            JSONArray dataList = responseObj.getJSONObject("data").getJSONObject("data").getJSONArray("country");
            for (Object d : dataList){
                JSONObject dd = (JSONObject) d;
                out.add(dd);
            }
        }
        return out;
    }

    private static List<JSONObject> getCompanyIndex() {
        //获取一级模块名称的源数据
        String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsBusiness/companyIndex";
        JSONObject header = new JSONObject();
        header.put("Referer", "https://www.gdzwfw.gov.cn/portal/legal/hot");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        JSONObject params = new JSONObject();
        params.put("region", "440000");

        int i = 3;
        String response = postFormReq(url, header, params);
        JSONObject data = JSONObject.parseObject(response);
        List<JSONObject> out = new ArrayList<>();
        if (data.getString("statusCode").equals("200")) {
            Object dataList = data.getJSONObject("data").getJSONArray("data").get(i);
            JSONObject dataObj = (JSONObject) dataList;
            JSONArray items = dataObj.getJSONArray("items");
            for (Object o : items){
                JSONObject oo = (JSONObject) o;
                out.add(oo);
            }
        }
        return out;
    }

    public static void main(String[] args) {
        Spider spider = new Spider();
        spider.getData(null, new ArrayList<>());
    }
}